<template>
  <e-table
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :border="false"
    :pageObj="null"
  >
    <el-table-column slot="sort" label="排序" width="100" align="center">
      <template slot-scope="scope">
        <el-input
          style="width: 65px"
          :max="100"
          size="mini"
          v-model="scope.row.positionOrder"
          @blur="handleOrderChange(scope.row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="岗位名称" slot="name" align="center">
      <template slot-scope="scope">
        <el-input
          style="width: 240px"
          class="fl"
          type="text"
          placeholder="请输入岗位名称"
          autocomplete="false"
          @blur="handleInputBlur(scope.row)"
          v-model="scope.row.positionName"
        />
        <!-- <span class="fl">{{ scope.row.positionName }}</span> -->
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { getPosition, deletePosition, updateOrder, updatePosition } from '@/api/position'
import { openNotification, orderChange } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      // 表头
      columns: [{ slot: 'name' }, { slot: 'sort' }, { slot: 'action' }],
      tableData: []
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    // 修改排序接口
    async handleOrderChange(row) {
      const res = orderChange(row, 'positionOrder', 'positionId', 'position')
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(record) {
      this.$nextTick(() => {
        this.$emit('edit', record)
      })
    },
    async handleInputBlur(row) {
      console.log('row', row)
      if (row.positionName) {
        const res = await updatePosition(row)
      } else {
        this.$message.error('岗位名称不能为空')
      }
    },
    // 删除
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.positionName}]记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deletePosition(row.positionId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await getPosition()
      const { data } = res
      this.tableData = data
      this.loading = false
    }
  }
}
</script>
